package com.springboot.service;

import com.alibaba.fastjson.JSON;
import com.hqj.ReturnUtil;
import com.springboot.dao.RoleDao;
import com.springboot.dao.UserDao;
import com.springboot.domain.NewUser;
import com.springboot.domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceI implements BaseService<NewUser> {

    @Autowired
    private UserDao ud;
    @Autowired
    private RoleDao rd;

    @Override
    public List<NewUser> find(NewUser newUser) {
        List<NewUser> list = ud.find(newUser);
        return list;
    }

    public List<Role> find(Role role){
        return rd.find(role);
    }

    public List<Role> findRoleMenus(Role role){
        return rd.roleByMenus(role);
    }


    @Override
    public NewUser get(NewUser newUser) {
        return null;
    }

    @Override
    public Map<String, Object> add(NewUser newUser) {
        if(newUser.getBirStr()!=null){
            try {
                newUser.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(newUser.getBirStr()));
            }catch (Exception e){
                System.out.println("时间转换错误!");
            }
        }
        int i = ud.add(newUser);
        return ReturnUtil.returnMap(i>0,"新增");
    }

    public Map<String, Object> add(String users) {
        //把字符串转换为json 对象在转为数组或集合
        List<NewUser> list = JSON.parseArray(users,NewUser.class);
        int i = ud.batchAdd(list);
        return ReturnUtil.returnMap(i>0,"批量添加");
    }

    @Override
    public Map<String, Object> edit(NewUser newUser) {
        return null;
    }

    @Override
    public Map<String, Object> remove(NewUser newUser) {
        return null;
    }

}
